package com.wangzefeng.shop.controller;

import com.wangzefeng.shop.entity.SysRole;
import com.wangzefeng.shop.pojo.CommonResult;
import com.wangzefeng.shop.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @Auther: wangzefeng
 * @Date: 2021/3/22 15:11
 * @Description:
 */
@RestController
@RequestMapping("/roles")
public class RoleController {

    @Autowired
    SysRoleService roleService;

    @GetMapping("/list")
    public CommonResult getRoleList(){
        List<SysRole> roleList = roleService.getRoleList(new HashMap<>());
        CommonResult successResult = CommonResult.getSuccessResult();
        successResult.setData(roleList);
        return successResult;
    }

    @GetMapping("/one/{roleId}")
    public CommonResult getRoleOne(@PathVariable("roleId") int roleId){
        SysRole role = roleService.getById(roleId);
        CommonResult successResult = CommonResult.getSuccessResult();
        successResult.setData(role);
        return successResult;
    }

    @DeleteMapping("/delete/{roleId}")
    public CommonResult deleteRoleOne(@PathVariable("roleId") int roleId){
        boolean b = roleService.removeById(roleId);
        CommonResult successResult = CommonResult.getSuccessResult();
        successResult.setData(b);
        return successResult;
    }

    @PutMapping("/edit")
    public CommonResult editRoleOne(SysRole sysRole){
        if(sysRole.getRoleId()==null)return CommonResult.getErrorResult(400,"未发现id，无法修改");
        boolean b = roleService.updateById(sysRole);
        CommonResult successResult = CommonResult.getSuccessResult();
        successResult.setData(b);
        return successResult;
    }

    @PostMapping("/save")
    public CommonResult saveRoleOne(SysRole sysRole){
        if(sysRole.getRoleId()!=null)return CommonResult.getErrorResult(400,"发生错误，无法保存");
        boolean b = roleService.save(sysRole);
        CommonResult successResult = CommonResult.getSuccessResult();
        successResult.setData(b);
        return successResult;
    }
}
